import axios from 'axios'

import config from '../config/index.js'

const service = axios.create({
	baseURL: config[import.meta.env.MODE].baseUrl,
	// baseURL:'http://localhost:8888/api/v1/',//如果直接使用后台接口，开发环境会报跨域，所以前端必须使用代理
	timeout: 6000,
	withCredentials:true,
	headers: { 'Content-Type': 'application/json;charset=utf-8' },
});

http://localhost:3000/api/v1/common/getCaptcha
service.interceptors.request.use(
	(config) => {
		const token = localStorage.getItem('token') || '';
		if (token) {
			config.headers['Authorization'] = 'Bearer ' +token;
		}
		return config;
	},
	(error) => {
		Promise.reject(error);
	},
);

service.interceptors.response.use(res => {
	if (typeof res.data !== 'object') {
		return Promise.reject(res)
	}
	if (res.status != 200) {
		
		return Promise.reject(res.data)
	}

	return res.data
})

export default service
